package com.tjsinfo.tjsoa.doc.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.doc.bo.ITDocDocBO;
import com.tjsinfo.tjsoa.doc.vo.TDocDoc;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class DocCheckListAction extends DispatchAction {
	private ITDocDocBO docBo;
	public void setDocBo(ITDocDocBO docBo) {
		this.docBo = docBo;
	}
	
	
	private List selectList(HttpServletRequest request,String node,Integer docMark,String docName) {
		WorkFlowTemplate test = new WorkFlowTemplate();
		List list2 = new ArrayList();
		Integer userId = ToolKit.getSessionUser(request).getUser_id();
		List list = test.selectTask(userId.toString(), node);
		Comparator comp = new Comparator(){
          public int compare(Object o1,Object o2) {
              TaskInstance p1=(TaskInstance)o1;
              TaskInstance p2=(TaskInstance)o2; 
             if(p1.getId()<p2.getId())
                 return 1;
             else
                 return 0;
             }
        };
        Collections.sort(list, comp);
        
        if(docName == null || "".equals(docName)){
    		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
    			TaskInstance taskInstance = (TaskInstance) iterator.next();
    			Long flowid =	taskInstance.getProcessInstance().getId();
    			
    			TDocDoc tdd = new TDocDoc();
    			tdd.setFlowid(Integer.parseInt(flowid.toString()));
    			tdd.setMark(docMark);
    			if(docBo.findByExample(tdd).size() > 0 && !"申请人修改".equals(taskInstance.getName()))
    			{
    				tdd = (TDocDoc) docBo.findByExample(tdd).get(0);
    				tdd.setTaskid(taskInstance.getId());
    				list2.add(tdd);
    			}
    		} 	
        }else{
    		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
    			TaskInstance taskInstance = (TaskInstance) iterator.next();
    			Long flowid =	taskInstance.getProcessInstance().getId();
    			
    			TDocDoc tdd = new TDocDoc();
    			tdd.setFlowid(Integer.parseInt(flowid.toString()));
    			tdd.setMark(docMark);
    			tdd.setDocName(docName);
    			if(docBo.findByExample(tdd).size() > 0 && !"申请人修改".equals(taskInstance.getName()))
    			{
    				tdd = (TDocDoc) docBo.findByExample(tdd).get(0);
    				tdd.setTaskid(taskInstance.getId());
    				list2.add(tdd);
    			}
    		}	
        }
		return list2;
	}	
	
	
	public ActionForward checkList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String node = request.getParameter("node");
		String docName = request.getParameter("docName");
		Integer docMark = Integer.parseInt(request.getParameter("docMark"));
		
		List<TDocDoc> list = selectList(request,node,docMark,docName);

		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();		
		
		for (TDocDoc tdd : list) {
			tdd.setDateTime1(sdf.format(tdd.getDateTime()));
			JSONObject tddJson = new JSONObject(tdd);
			array.put(tddJson);
		}
		
		object.put("totalProperty",list.size());
		object.put("root", array);
		
		response.getWriter().print(object);
		
		return null;
	}
	
	public ActionForward checkSelectList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		List<TDocDoc> list = null;
		Integer start = 0;
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		String columnName = request.getParameter("docName");
		Integer docMark = Integer.parseInt(request.getParameter("docMark"));
		
		if (columnName == null || columnName == "") {
			list = docBo.cheakOverAll(docMark,request);
		} else {
			list = docBo.cheakOverColumn(docMark, "docName", columnName, request);
		}
		
		for (TDocDoc tdd : list) {
			JSONObject giJson = new JSONObject(tdd);
			array.put(giJson);
		}
		
		object.put("totalProperty",docBo.cheakOverTotal());
		object.put("root", array);
		response.getWriter().print(object);
		
		return null;
	}
}
